package demo1;

import java.net.PortUnreachableException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: liangyuanzhang
 * Date: 2025-3-12
 * Time: 8:20
 */
// 写一个泛型类 当中有个方法 可以求数组最大值
class Alg<E extends Comparable<E>>{

    public E findMax(E[] array){
        E max = array[0];
        for (int i = 0; i < array.length; i++) {
            //if (max < array[i]){
            if (max.compareTo(array[i]) < 0){
                max = array[i];
            }
        }
        return max;
    }
}
class Alg2{
    //泛型方法
    public<E extends Comparable<E>> E findMax(E[] array){
        E max = array[0];
        for (int i = 0; i < array.length; i++) {
            //if (max < array[i]){
            if (max.compareTo(array[i]) < 0){
                max = array[i];
            }
        }
        return max;
    }
}
class Alg3{
    public static <E extends Comparable<E>> E findMax(E[] array){
        E max = array[0];
        for (int i = 0; i < array.length; i++) {
            //if (max < array[i]){
            if (max.compareTo(array[i]) < 0){
                max = array[i];
            }
        }
        return max;
    }
}
public class TestGeneric {
    public static void main(String[] args) {
        Integer[] array = {1,2,3,4,45,5,6,8};
        System.out.println(Alg3.<Integer>findMax(array));
    }
    public static void main2(String[] args) {
        Alg alg2 = new Alg();
        Integer[] array = {1,2,3,4,45,5,6,8};
        System.out.println(alg2.<Integer>findMax(array));// 使用静态方法可以不new对象，直接通过类名访问
    }
    public static void main1(String[] args) {
        Integer[] array = {1,2,3,4,45,5,6,8};
        Alg<Integer> alg = new Alg<>();
        int a = alg.findMax(array);
        System.out.println(a);
    }
}
